package com.songlanyun.common.utils;

import lombok.extern.slf4j.Slf4j;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 反射工具类
 */
@Slf4j
public class ReflectioinUtil {

    /**
     * 获取实体类 @Column 的name
     *
     * @param clazz
     * @return
     */
    public static Map<String, String> getColumnNameMap(Class<?> clazz) {
        Map<String, String> entityFieldsMap = new HashMap<>();
        Field[] videoFields = clazz.getDeclaredFields();
        Column presentColumn;
        String columnName;
        for (Field field : videoFields) {
            if (field.isAnnotationPresent(Column.class)) {
                presentColumn = field.getDeclaredAnnotation(Column.class);
                columnName = presentColumn.name();
                entityFieldsMap.put(columnName, field.getName());
            }
        }
        return entityFieldsMap;
    }

    /**
     * 获取实体类 @Column 的name
     *
     * @param clazz
     * @return
     */
    public static List<String> getColumnNameList(Class<?> clazz) {
        List<String> columnNameList = new ArrayList<>();
        Field[] videoFields = clazz.getDeclaredFields();
        Column presentColumn;
        String columnName;
        for (Field field : videoFields) {
            if (field.isAnnotationPresent(Column.class)) {
                presentColumn = field.getDeclaredAnnotation(Column.class);
                columnName = presentColumn.name();
                columnNameList.add(columnName);
            }
        }
        return columnNameList;
    }

    /**
     * @param o
     * @return
     * @desc 获取对象所有field字段值
     */
    public static List<String> getFieldValueList(Object o) {
        List<String> list = new ArrayList<String>();
        String[] fieldNames = getFiledName(o);
        for (int i = 0; i < fieldNames.length; i++) {
            String name = fieldNames[i];
            Object value = getFieldValueByName(name, o);
            if (null != value) {
                if (getFiledType(name, o).equals(Date.class)) {
                    value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value);
                }
                list.add(value.toString());
            }
        }
        return list;
    }

    public static List<String> getFieldValueStr(Object o) {
        List<String> list = new ArrayList<String>();
        String[] fieldNames = getFiledName(o);
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < fieldNames.length; i++) {
            String name = fieldNames[i];
            Object value = getFieldValueByName(name, o);
            if (null != value) {
                if (getFiledType(name, o).equals(Date.class)) {
                    value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value);
                }
                list.add("\""+value.toString()+"\"");

            }
        }
        return list;// StringUtils.join(list.toArray(), ",")+"\n";
    }
    /**
     * @param clazz
     * @return
     * @desc 获取属性名数组
     */
    public static String[] getFiledName(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * @param o
     * @return
     * @desc 获取属性名数组
     */
    private static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * @param fieldName
     * @param o
     * @return
     * @desc 根据属性名获取属性值
     */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            log.error("获取属性值失败！" + e, e);
        }
        return null;
    }

    /**
     * @param fieldName
     * @param o
     * @return
     * @desc 获取属性的数据类型
     */
    private static Object getFiledType(String fieldName, Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (Objects.equals(fieldName, field.getName())) {
                return field.getType();
            }
        }
        return null;
    }
}
